package com.fingard.dsp.bank.directbank.bozz02;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 郑州银行余额查询
 * 
 * @author Cruise
 * @date 2018/11/19
 */
public class BOZZ02Balance extends BOZZ02Base implements IBalance{
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
        retAts.writeStart();
        
        try {
        	BOZZ02Header tmpBankHeader = new BOZZ02Header();
            tmpBankHeader.trsCode = "B2EActBalQry";
            
        	while(p_req7506.readRecord()) {
                tmpBankHeader.erpTrsTimestamp = Format.DateTimeFormat.format(new Date());
                tmpBankHeader.erpJnlNo = getQryTransID();
                
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHead(xmlWriter, tmpBankHeader);
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("AcNo", p_req7506.readedDetail.actNum);
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), "B2EActBalQry", true,"0");

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRespCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        if ("000000".equals(tmpRespCode)) {
                            tmpRetDetail.respCode = "0";
                            
                            tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
                            tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                            tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                            
                            tmpRetDetail.currBal = tmpBkReader.getFirstText("/Message/Body/Balance");
                            tmpRetDetail.availBal = tmpBkReader.getFirstText("/Message/Body/AvailBal");
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                            if (StringHelper.hasAnyChar(tmpBkReader.getFirstText("/transaction/error"))) {
                            	tmpRetDetail.respInfo = tmpBkReader.getFirstText("/transaction/error");
                            }
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[0];
                }
            }
        	retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
        }  catch (Exception ex) {
        	ex.printStackTrace();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(ex.getMessage());
            WriteBankLogLn(ex);
        }
        
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
